package com.guigu.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.dao.CheckGroupMapper;
import com.guigu.health.dao.CheckGroup_CheckItemMapper2;
import com.guigu.health.dao.SetmealMapper;
import com.guigu.health.pojo.*;
import com.guigu.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName:SetemealServiceImpl
 * @Description:套餐业务的实现类
 * @Author:longtao
 * @Date:2021/3/30
 * @Version:1.0
 */
@Service("setemealService")
public class SetemealServiceImpl implements SetmealService {

    @Autowired
    @Qualifier(value = "setmealMapper")
    public SetmealMapper setmealMapper;


    public CheckGroup_CheckItemMapper2 checkGroup_checkItemMapper2;
    @Autowired
    @Qualifier(value = "checkGroup_CheckItemMapper2")
    public void setCheckGroup_checkItemMapper2(CheckGroup_CheckItemMapper2 checkGroup_checkItemMapper2) {
        this.checkGroup_checkItemMapper2 = checkGroup_checkItemMapper2;
    }

    public CheckGroupMapper checkGroupMapper;
    @Autowired
    @Qualifier(value = "checkGroupMapper")
    public void setCheckGroupMapper(CheckGroupMapper checkGroupMapper) {
        this.checkGroupMapper = checkGroupMapper;
    }


    @Override
    public List<Setmeal> selectByExample(SetmealExample example) {
        return setmealMapper.selectByExample(example);
    }

    @Override
    public List<SetMemberDetails> querySetMealByAccount(String account) {
        return setmealMapper.querySetMealByAccount(account);
    }

    @Override
    public List<Map<String, Object>> numberOfBookingPackages() {
        return setmealMapper.packageReservationNumber();
    }

    @Override
    public PageInfo<Setmeal> queryAllByName(SetmealExample name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Setmeal> list = setmealMapper.selectByExample(name);
        PageInfo<Setmeal> pageInfo=new PageInfo<Setmeal>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public boolean updateSetMeal(Setmeal setmeal, SetmealExample example,int [] checkGroupIds) {
        boolean flag=false;
        setmealMapper.deleteSetMealKey(setmeal.getId());
        if (null!=checkGroupIds){
            for (int i : checkGroupIds) {
                setmealMapper.addSetMealKey(setmeal.getId(),i);
            }
        }
        int i = setmealMapper.updateByExampleSelective(setmeal, example);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean deleteSetMeal(Integer id) {
        boolean flag=false;
        /**
         * 删除它与检查组中的关系
         */
        setmealMapper.deleteSetMealKey(id);
        int i = setmealMapper.deleteByPrimaryKey(id);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean addSetMealCheckGroup(List<Setmeal_CheckgroupKey> list) {
       boolean flag=false;

        int i = setmealMapper.addSetMealCheckGroup(list);
        if (i==list.size()){
            flag=true;
        }
        return flag;
    }

    @Override
    public List<CheckGroup_CheckItem> queryAllCheckGroupCheckItem() {
        List<CheckGroup_CheckItem> checkItems = checkGroup_checkItemMapper2.queryAll();
        return checkItems;
    }

    @Override
    public boolean addSetMeal(Setmeal setmeal) {
        boolean flag=false;
        int i = setmealMapper.insert(setmeal);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    /**
     * 显示检查组
     *
     * @return
     */
    @Override
    public List<CheckGroup> queryAllCheckGroup() {
        List<CheckGroup> list = checkGroupMapper.selectByExample(null);
        return list;
    }

    /**
     * 根据套餐id显示要选中的检查组id
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getCountCheckGroupIds(int id) {
        List<Integer> ids = setmealMapper.getSetMealIds(id);
        return ids;
    }

    /**
     * 编码名不一致
     *
     * @param code
     * @return
     */
    @Override
    public int count(String code) {
        return setmealMapper.count(code);
    }
}
